$(function() {
	"use strict";
	var initMenuTree = function() {

		$("#menu_tree").jstree({
			"core" : {
				"themes" : {
					"responsive" : false
				},
				"check_callback" : true,
				'data' : {
					'url' : function(node) {
						return context + '/console/menu/tree/data';
					},
					'data' : function(node) {
						return {
							'parent' : node.id
						};
					}
				}
			},
			"types" : {
				"default" : {
					"icon" : "fa fa-folder icon-state-warning icon-lg"
				},
				"file" : {
					"icon" : "fa fa-file icon-state-warning icon-lg"
				}
			},
			"plugins" : [ "contextmenu", "dnd", "types" ],
			"contextmenu" : {
				"items" : {
					"新增" : {
						"label" : "新增",
						"action" : function(data) {
							var inst = jQuery.jstree.reference(data.reference),  
		                    obj = inst.get_node(data.reference);
							
							var dialog = $("#menuEditDialog");
							var form = $("#menuForm",dialog);
							form[0].reset();
							$("input[name='menuParent']",form).val(obj.id);
							$(".modal-title",dialog).html('新增菜单【父菜单：'+obj.text+'】');
							
							$(".ok",dialog).unbind('click').on('click',function(){
								
								App.blockUI({
					                target: '#menuEditDialogContent',
					                boxed: true,
					                message: '数据处理中...',
					                textOnly: true
					            });
								
								$.ajax({
						            url:context+'/console/menu/save',
						            type:'POST',
						            data:form.serialize(),
						            dataType:'json',
						            success:function(data,textStatus,jqXHR){
						            	App.unblockUI('#menuEditDialogContent');
						                if(data.menuId){
						                	toastr["info"]("保存菜单成功", "提示");
						                	inst.create_node(obj,{
					                			id:data.menuId,
					                			text:data.menuName,
					                			state:{opened:false}
					                		}
						                	);
						                	inst.open_node(obj);
						                	dialog.modal('hide');
						                }else{
						                	toastr["error"]("保存菜单异常", "提示");
						                }
						                
						            }
						        });
							});
							
							dialog.modal();
						}
					},
					"编辑":{
						"label":"编辑",
						"action" : function(data) {
							var inst = jQuery.jstree.reference(data.reference),  
		                    obj = inst.get_node(data.reference);
							if(obj.id==1){
								toastr["warning"]("根菜单不能编辑", "提示");
								return;
							}
							
							var dialog = $("#menuEditDialog");
							var form = $("#menuForm",dialog);
							form[0].reset();
							$.ajax({
					            url:context+'/console/menu/load/'+obj.id,
					            type:'POST',
					            dataType:'json',
					            success:function(data,textStatus,jqXHR){
					                if(data.success){
					                	iTsai.form.deserialize(form,data.data)
					                	
					                	
					                	$(".modal-title",dialog).html('编辑菜单【'+obj.text+'】');
										
										$(".ok",dialog).unbind('click').on('click',function(){
											
											App.blockUI({
								                target: '#menuEditDialogContent',
								                boxed: true,
								                message: '数据处理中...',
								                textOnly: true
								            });
											
											$.ajax({
									            url:context+'/console/menu/save',
									            type:'POST',
									            data:form.serialize(),
									            dataType:'json',
									            success:function(data,textStatus,jqXHR){
									            	App.unblockUI('#menuEditDialogContent');
									                if(data.menuId){
									                	toastr["info"]("保存菜单成功", "提示");
									                	inst.rename_node(obj,data.menuName);
									                	dialog.modal('hide');
									                }else{
									                	toastr["error"]("保存菜单异常", "提示");
									                }
									                
									            }
									        });
										});
										
										dialog.modal();
					                	
					                }else{
					                	toastr["error"]("加载菜单数据异常", "提示");
					                }
					                
					            }
					        });
							
							
						}
					},
					"删除":{
						"label":"删除",
						"action" : function(data) {
							var inst = jQuery.jstree.reference(data.reference),  
		                    obj = inst.get_node(data.reference);
							if(obj.id==1){
								toastr["warning"]("根菜单不能删除", "提示");
								return;
							}
							
							$.ajax({
					            url:context+'/console/menu/delete/'+obj.id,
					            type:'POST',
					            dataType:'json',
					            success:function(data,textStatus,jqXHR){
					                if(data.success){
					                	toastr["info"]("删除菜单成功", "提示");
					                	inst.delete_node (obj);
					                }else{
					                	toastr["error"]("删除菜单异常", "提示");
					                }
					                
					            }
					        });
						}
					}
				}
			}
		}).bind('changed.jstree',function(node,action){
			$("#menuViewForm")[0].reset();
			App.blockUI({
                target: '#menuViewForm',
                boxed: true,
                message: '数据加载中...'
            });
			$.ajax({
	            url:context+'/console/menu/load/'+action.node.id,
	            type:'POST',
	            dataType:'json',
	            success:function(data,textStatus,jqXHR){
	            	App.unblockUI('#menuViewForm');
	                if(data.success){
	                	iTsai.form.deserialize($('#menuViewForm'),data.data)
	                }else{
	                	toastr["error"]("加载菜单数据异常", "提示");
	                }
	                
	            }
	        });
			
		});

	}

	initMenuTree();
});